package com.main;

import java.awt.BorderLayout;
import java.awt.Color;
import java.awt.EventQueue;
import java.awt.ScrollPane;
import java.awt.event.ActionEvent;
import java.awt.event.ActionListener;
import java.io.BufferedInputStream;
import java.io.BufferedWriter;
import java.io.File;
import java.io.FileInputStream;
import java.io.FileNotFoundException;
import java.io.FileWriter;
import java.io.IOException;
import java.text.ParseException;
import java.text.SimpleDateFormat;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

import javax.swing.JButton;
import javax.swing.JFileChooser;
import javax.swing.JFrame;
import javax.swing.JMenu;
import javax.swing.JMenuBar;
import javax.swing.JTextArea;
import javax.swing.JTextPane;
import javax.swing.filechooser.FileFilter;

import org.apache.poi.xssf.usermodel.XSSFRow;
import org.apache.poi.xssf.usermodel.XSSFSheet;
import org.apache.poi.xssf.usermodel.XSSFWorkbook;

public class Main {

	private JFrame frame;
	JTextPane textPane = null;
	static JTextArea textArea = null;
	ScrollPane scrollPane;
	JFileChooser jFileChooser;

	/**
	 * Launch the application.
	 */
	public static void main(String[] args) {
		EventQueue.invokeLater(new Runnable() {
			public void run() {
				try {
					Main window = new Main();
					window.frame.setVisible(true);
				} catch (Exception e) {
					e.printStackTrace();
				}
			}
		});
	}

	/**
	 * Create the application.
	 */
	public Main() {
		initialize();

	}

	/**
	 * Initialize the contents of the frame.
	 */
	private void initialize() {
		textPane = new JTextPane();
		textPane.setForeground(Color.RED);
		textPane.setBackground(Color.WHITE);
		frame = new JFrame();
		frame.setBounds(100, 100, 800, 800);
		frame.setDefaultCloseOperation(JFrame.EXIT_ON_CLOSE);

		JMenuBar menuBar = new JMenuBar();
		frame.setJMenuBar(menuBar);

		JMenu mnNewMenu = new JMenu("\u83DC\u5355");
		menuBar.add(mnNewMenu);

		JMenu menu = new JMenu("\u5DE5\u5177");
		menuBar.add(menu);
		textArea = new JTextArea();
		textArea.setForeground(Color.DARK_GRAY);
		jFileChooser = new JFileChooser(new File("C:\\Users\\Administrator\\Desktop"));
		jFileChooser.setMultiSelectionEnabled(true);
		jFileChooser.setFileFilter(new FileFilter() {

			@Override
			public String getDescription() {
				// TODO Auto-generated method stub

				return "支持文件类型(*.xlsx)";
			}

			@Override
			public boolean accept(File f) {
				// TODO Auto-generated method stub
				String name = f.getName();
				if ( name.endsWith(".xlsx")) {
					return true;

				}
				return false;
			}
		});
		jFileChooser.setFileSelectionMode(JFileChooser.FILES_AND_DIRECTORIES);
		JButton btnNewButton = new JButton("选择考勤文件");

		btnNewButton.addActionListener(new ActionListener() {
			public void actionPerformed(ActionEvent arg0) {
				jFileChooser.showOpenDialog(null);
			}
		});
		jFileChooser.addActionListener(new ActionListener() {

			@Override
			public void actionPerformed(ActionEvent ev) {
				// TODO Auto-generated method stub
				try {
					File[] f = jFileChooser.getSelectedFiles();
					printLog("选择的文件是："+f[0].getPath());
					printLog("正在统计数据……");
					handlerFiles(f);
					
				} catch (Exception e) {
					textPane.setText(e.toString());
				}
			}

		});
		scrollPane = new ScrollPane();
		scrollPane.add(textArea);
		frame.getContentPane().add(scrollPane, BorderLayout.CENTER);
		frame.getContentPane().add(btnNewButton, BorderLayout.NORTH);
		frame.getContentPane().add(textPane, BorderLayout.SOUTH);

	}

	private static void printLog(final String str) {
		new Thread(new Runnable() {// 启动数据获取，更新显示进程

			public void run() {
				textArea.append(str + "\n\n");
				textArea.paintImmediately(textArea.getBounds());
				textArea.setLineWrap(true); // 激活自动换行功能
				textArea.setWrapStyleWord(true); // 激活断行不断字功能
				textArea.setCaretPosition(textArea.getText().length());
			}

		}).start(); //
	}

	public void handlerFiles(File[] files) throws FileNotFoundException {
		File file = files[0];
		try {
			readExcel1(file);
		} catch (IOException e) {
			// TODO Auto-generated catch block
			printLog(e.getMessage());
		} catch (ParseException e) {
			// TODO Auto-generated catch block
			printLog(e.getMessage());
		}
	}
	
	public static List<String> readExcel1(File file) throws IOException, ParseException{
	      List<String> id = new ArrayList<String>();
		//xlsx XSSFWorkbook;xls HSSFWorkbook
		XSSFWorkbook excel = new XSSFWorkbook(new BufferedInputStream(new FileInputStream(file)));
		XSSFSheet sheet = excel.getSheetAt(0);
		//定义行
		XSSFRow row;
		//定义列数据
		String cell;
		StringBuilder sb;
		String name=null;
		SimpleDateFormat sm=new SimpleDateFormat("yyyy-MM-dd HH:mm:ss");
		SimpleDateFormat sm2=new SimpleDateFormat("yyyy-MM-dd");
		//循环每一个sheet中的每一行
		for (int i = 0; i < sheet.getPhysicalNumberOfRows(); i++) {
			sb=null;
			sb=new StringBuilder();
			row=sheet.getRow(i);
			int wordDay=0,late=0,leaveEarly=0;
			 int  total=0;
		     if(i>=1) {
		    	 if(i%2==0) {
		    		
//		    		 System.out.println(row.getCell(0)+" "+row.getCell(2)+" "+row.getCell(8)+" "+row.getCell(10)+" "+row.getCell(18)+" "+row.getCell(20));
		    		 name=row.getCell(0)+" "+row.getCell(2)+" "+row.getCell(8)+" "+row.getCell(10)+" "+row.getCell(18)+" "+row.getCell(20);
		    		 sb.append("_______________________________________________________________________");
		    	 }else {
		    		
		    		 for(int z=0;z<row.getPhysicalNumberOfCells();z++){
//		 				//循环每一行中的所有列,就是得到单元格中的数据 
		    			 String[] times = row.getCell(z).toString().split(":");
		    			 
		    			  if(times.length==2){
		    				 System.out.println((z+1)+"号,打卡1次"+row.getCell(z));
		    				 total=total+1;
		    				 wordDay++;
		    				 long realtime = sm.parse(sm2.format(new Date())+" "+row.getCell(z).toString()+":00").getTime();
		    				 long comparetimeAm = sm.parse(sm2.format(new Date())+" 08:30:00").getTime();
		    				 long comparetimePm = sm.parse(sm2.format(new Date())+" 18:00:00").getTime();
		    				 if(realtime>comparetimePm||realtime<comparetimeAm) {
		    					
		    				 }
		    				 if(realtime>comparetimeAm&&realtime<comparetimeAm) {
		    					// late++;
		    					 //旷工+早退
		    				 }
		    				 if(realtime<comparetimeAm&&realtime<comparetimeAm) {
		    					// late++;
		    				 }
		    				
		    			 }else if(times.length==3){
//		    				 System.out.println((z+1)+"号,打卡2次"+row.getCell(z));
//		    				 System.out.println(row.getCell(z).toString().substring(0,5));
//		    				 System.out.println(row.getCell(z).toString().substring(5));
		    				 
		    				 long realtimeAm = sm.parse(sm2.format(new Date())+" "+row.getCell(z).toString().substring(0,5)+":00").getTime();
		    				 long comparetimeAm = sm.parse(sm2.format(new Date())+" 08:30:00").getTime();
//		    				 System.out.println(realtimeAm>comparetimeAm==true?"早上迟到":"正常");
		    				 
		    				 if(realtimeAm>comparetimeAm) {
		    					 late++; 
//		    					 System.out.println((z+1)+"号,迟到："+row.getCell(z).toString().substring(0,5));
		    					 sb.append((z+1)+"号,迟到："+row.getCell(z).toString().substring(0,5)+"\r");
		    				 }
		    				 long realtimePm = sm.parse(sm2.format(new Date())+" "+row.getCell(z).toString().substring(5)+":00").getTime();
		    				 long comparetimePm = sm.parse(sm2.format(new Date())+" 18:00:00").getTime();
//		    				 System.out.println(realtimePm<comparetimePm==true?"下午早退":"正常");
		    				 if(realtimePm<comparetimePm) {
		    					 leaveEarly++; 
//		    					 System.out.println((z+1)+"号,早退："+row.getCell(z).toString().substring(5));
		    					 sb.append((z+1)+"号,早退："+row.getCell(z).toString().substring(5)+"\r");
		    				 }
		    				 
		    				 total=total+2;
		    				 wordDay++;
		    				 
		    			 }else if(times.length==4){
//		    				 System.out.println((z+1)+"号,打卡3次"+row.getCell(z));
//		    				 System.out.println(row.getCell(z).toString().substring(0,5));
//		    				 System.out.println(row.getCell(z).toString().substring(10));
		    				 long realtimeAm = sm.parse(sm2.format(new Date())+" "+row.getCell(z).toString().substring(0,5)+":00").getTime();
		    				 long comparetimeAm = sm.parse(sm2.format(new Date())+" 08:30:00").getTime();
//		    				 System.out.println(realtimeAm>comparetimeAm==true?"早上迟到":"正常");
		    				 
		    				 if(realtimeAm>comparetimeAm) {
		    					 late++; 
//		    					 System.out.println((z+1)+"号,迟到："+row.getCell(z).toString().substring(0,5));
		    					 sb.append((z+1)+"号,迟到："+row.getCell(z).toString().substring(0,5)+"\r");
		    				 }
		    				 long realtimePm = sm.parse(sm2.format(new Date())+" "+row.getCell(z).toString().substring(10)+":00").getTime();
		    				 long comparetimePm = sm.parse(sm2.format(new Date())+" 18:00:00").getTime();
//		    				 System.out.println(realtimePm<comparetimePm==true?"下午早退":"正常");
		    				 if(realtimePm<comparetimePm) {
		    					 leaveEarly++; 
//		    					 System.out.println((z+1)+"号,早退："+row.getCell(z).toString().substring(10));
		    					 sb.append((z+1)+"号,早退："+row.getCell(z).toString().substring(10)+"\r");
		    				 }
		    				 
		    				 total=total+3;
		    				 wordDay++;
		    			 }else if(times.length==5){
//		    				 System.out.println((z+1)+"号,打卡4次"+row.getCell(z));
//		    				 System.out.println(row.getCell(z).toString().substring(0,5));
//		    				 System.out.println(row.getCell(z).toString().substring(15));
		    				 long realtimeAm = sm.parse(sm2.format(new Date())+" "+row.getCell(z).toString().substring(0,5)+":00").getTime();
		    				 long comparetimeAm = sm.parse(sm2.format(new Date())+" 08:30:00").getTime();
//		    				 System.out.println(realtimeAm>comparetimeAm==true?"早上迟到":"正常");
		    				 
		    				 if(realtimeAm>comparetimeAm) {
		    					 late++; 
//		    					 System.out.println((z+1)+"号,迟到："+row.getCell(z).toString().substring(0,5));
		    					 sb.append((z+1)+"号,迟到："+row.getCell(z).toString().substring(0,5)+"\r");
		    				 }
		    				 long realtimePm = sm.parse(sm2.format(new Date())+" "+row.getCell(z).toString().substring(15)+":00").getTime();
		    				 long comparetimePm = sm.parse(sm2.format(new Date())+" 18:00:00").getTime();
//		    				 System.out.println(realtimePm<comparetimePm==true?"下午早退":"正常");
		    				 if(realtimePm<comparetimePm) {
		    					 leaveEarly++; 
//		    					 System.out.println((z+1)+"号,早退："+row.getCell(z).toString().substring(15));
		    					 sb.append((z+1)+"号,早退："+row.getCell(z).toString().substring(15)+"\r");
		    				 }
		    				 
		    				 total=total+4;
		    				 wordDay++;
		    			 }else if(times.length==6){
//		    				 System.out.println((z+1)+"号,打卡5次"+row.getCell(z));
//		    				 System.out.println(row.getCell(z).toString().substring(0,5));
//		    				 System.out.println(row.getCell(z).toString().substring(20));
		    				 long realtimeAm = sm.parse(sm2.format(new Date())+" "+row.getCell(z).toString().substring(0,5)+":00").getTime();
		    				 long comparetimeAm = sm.parse(sm2.format(new Date())+" 08:30:00").getTime();
//		    				 System.out.println(realtimeAm>comparetimeAm==true?"早上迟到":"正常");
		    				 
		    				 if(realtimeAm>comparetimeAm) {
		    					 late++; 
//		    					 System.out.println((z+1)+"号,迟到："+row.getCell(z).toString().substring(0,5));
		    					 sb.append((z+1)+"号,迟到："+row.getCell(z).toString().substring(0,5)+"\r");
		    				 }
		    				 long realtimePm = sm.parse(sm2.format(new Date())+" "+row.getCell(z).toString().substring(20)+":00").getTime();
		    				 long comparetimePm = sm.parse(sm2.format(new Date())+" 18:00:00").getTime();
//		    				 System.out.println(realtimePm<comparetimePm==true?"下午早退":"正常");
		    				 if(realtimePm<comparetimePm) {
		    					 leaveEarly++; 
//		    					 System.out.println((z+1)+"号,早退："+row.getCell(z).toString().substring(20));
		    					 sb.append((z+1)+"号,早退："+row.getCell(z).toString().substring(20)+"\r");
		    				 }
		    				 
		    				 total=total+5;
		    				 wordDay++;
		    			 }else if(times.length==7){
//		    				 System.out.println((z+1)+"号,打卡6次"+row.getCell(z));
//		    				 System.out.println(row.getCell(z).toString().substring(0,5));
//		    				 System.out.println(row.getCell(z).toString().substring(25));
		    				 
		    				 long realtimeAm = sm.parse(sm2.format(new Date())+" "+row.getCell(z).toString().substring(0,5)+":00").getTime();
		    				 long comparetimeAm = sm.parse(sm2.format(new Date())+" 08:30:00").getTime();
//		    				 System.out.println(realtimeAm>comparetimeAm==true?"早上迟到":"正常");
		    				 
		    				 if(realtimeAm>comparetimeAm) {
		    					 late++; 
//		    					 System.out.println((z+1)+"号,迟到："+row.getCell(z).toString().substring(0,5));
		    					 sb.append((z+1)+"号,迟到："+row.getCell(z).toString().substring(0,5)+"\r");
		    				 }
		    				 long realtimePm = sm.parse(sm2.format(new Date())+" "+row.getCell(z).toString().substring(25)+":00").getTime();
		    				 long comparetimePm = sm.parse(sm2.format(new Date())+" 18:00:00").getTime();
//		    				 System.out.println(realtimePm<comparetimePm==true?"下午早退":"正常");
		    				 if(realtimePm<comparetimePm) {
		    					 leaveEarly++; 
//		    					 System.out.println((z+1)+"号,早退："+row.getCell(z).toString().substring(25));
		    					 sb.append((z+1)+"号,早退："+row.getCell(z).toString().substring(25)+"\r");
		    				 }
		    				 total=total+6;
		    				 wordDay++;
		    			 }
		 			}
		    		 sb.append(name+" 共打卡:"+total+"次；共上班="+wordDay+"天"+";共迟到："+late+"次，早退："+leaveEarly+"次"+"\r");
//		    		 System.out.println(name+" 共打卡:"+total+"次；共上班="+wordDay+"天"+";共迟到："+late+"次，早退："+leaveEarly+"次"+"\r");
		    	 }
		    	 
		      }
		     //System.out.println(sb.toString());
		     saveFile(sb.toString());
			id.add(sb.toString()+"\r");
		}
		//file.delete();
//		System.out.println(id.toString());
		printLog("文件已经自动保存在：C:\\Users\\Administrator\\Desktop\\考勤结果.doc");
		return id;
		}
	
	static void saveFile( String content){
		try {


			   File file = new File("C:\\Users\\Administrator\\Desktop\\考勤结果.doc");

			   // if file doesnt exists, then create it
			   if (!file.exists()) {
			    file.createNewFile();
			   }

			   FileWriter fw = new FileWriter(file.getAbsoluteFile(),true);
			   BufferedWriter bw = new BufferedWriter(fw);
			   bw.write(content+"\n");
			   bw.close();

			  } catch (IOException e) {
			   e.printStackTrace();
			  }
		 }
	
}
